package com.demo2do.fms.admin.web.controller.supplier.sub.info;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.search.supplier.TeamSearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.supplier.TeamService;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.entity.driver.Team;

/**
 * 分属供方车队信息查询
 * 
 * 业务逻辑：
 * 查询分属供方车队信息
 * 
 * - 总部、分区：可看所有车队信息
 * - 一级供方：可看所有属于该一级供方的二级供方的车队信息，自己车队的信息在车队信息管理
 * - 二级供方： 无可看
 * 
 * Created by zhonghui on 2017/6/21.
 */
@Controller
@RequestMapping("/sub-teams")
@MenuSetting("menu-supplier")
public class SubTeamController {

    @Autowired
    private TeamService teamService;

    /**
     * list 分属供方车队信息
     *
     * @param login
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-supplier-team-info")
    public ModelAndView list(@Secure Login login, Page page) {

        ModelAndView modelAndView = new ModelAndView("supplier/sub/info/team/sub-team-list");

        int totals = teamService.countSub(login, page);
        if (totals > 0) {

            List<Team> teams = teamService.listSub(login, page.with(totals));

            modelAndView.addObject("teams", teams);
            modelAndView.addObject("page", page);
            
        }

        return modelAndView;
        
    }

    /**
     * search 分属供方车队信息查询
     * 
     * @param login
     * @param page
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @MenuSetting("submenu-supplier-team-info")
    public ModelAndView search(Page page) {
        ModelAndView modelAndView = new ModelAndView("supplier/sub/info/team/sub-team-search");
        return modelAndView;
    }

    /**
     * on search
     * 
     * @param login
     * @param teamSearch
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-supplier-team-info")
    public ModelAndView onSearch(@Secure Login login, TeamSearch teamSearch, 
                                 HttpServletRequest request, Page page) {

        ModelAndView modelAndView = new ModelAndView("supplier/sub/info/team/sub-team-list");

        int totals = teamService.countSub(teamSearch, login);
        if (totals > 0) {

            List<Team> teams = teamService.searchSub(teamSearch, login, page.with(totals));
            modelAndView.addObject("teams", teams);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));

        }

        return modelAndView;
        
    }

    /**
     * view 详情
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @MenuSetting("submenu-supplier-team-info")
    public ModelAndView view(@PathVariable("id") Long id) {
        
        ModelAndView modelAndView = new ModelAndView("supplier/sub/info/team/sub-team-view");
        
        Team team = teamService.load(id);
        
        modelAndView.addObject("team", team);
        
        return modelAndView;
        
    }
    
}